home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / Stopwatch2.3 / Source / createPath.c < prev    next >
Text File  |  1995-06-12  |  1KB  |  59 lines

  1. #import <errno.h>
  2. #import <string.h>
  3. #import <stdio.h>
  4. #import <libc.h>
  5. #import <sys/types.h>
  6. #import <sys/stat.h>
  7. #import <objc/hashtable.h>    /* for NXCopyStringBuffer */
  8. #import "createPath.h"
  9.  
  10. /*
  11.  * Check each path element by iterating over the string, substituting
  12.  * a null byte for each slash and checking that the intermediate
  13.  * directory exists or can be created. The original string is uneffected.
  14.  */
  15. CreatePathErr 
  16. createPath( const char *callersPath, int mode )
  17. {
  18.   char *slash, *ptr, *path;
  19.   CreatePathErr err = PathCreationOk;
  20.  
  21.   ptr = path = NXCopyStringBuffer(callersPath);
  22.   
  23.   /*
  24.    * No need to check root, plus it makes for
  25.    * an ugly boundary condition!
  26.    */
  27.   if ( *ptr == '/' )
  28.     ++ptr;
  29.  
  30.   do {
  31.     struct stat statbuf;
  32.  
  33.     if ( (slash = strchr( ptr, '/' )) != NULL ) {
  34.       *slash = '\0';        /* truncate it here for testing */
  35.       ptr = slash + 1;        /* advance ptr for next iteration */
  36.     }
  37.  
  38.     if ( stat( path, &statbuf ) < 0 ) {
  39.  
  40.       /* if the dir doesn't exist, try to create it */
  41.       if ( errno == ENOENT ) {
  42.     if ( mkdir( path, mode ) < 0 )
  43.       err = PathMkdirFailure;
  44.       } else
  45.     err = PathStatFailure;
  46.  
  47.     } else            /* stat(2) succeeded */
  48.       if ( ! (statbuf.st_mode & S_IFDIR) )
  49.     err = PathNotDirectory;
  50.  
  51.     if ( slash )
  52.       *slash = '/';        /* restore the slash */
  53.  
  54.   } while ( err == PathCreationOk && slash != NULL );
  55.  
  56.   free(path);
  57.   return err;
  58. }
  59.